/*
 * Debts Manager. Simple android application for managing debts.
 * Copyright (C) 2010 Maxim Manuylov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
 */

package manuylov.maxim.dm.data.object;

import manuylov.maxim.dm.Constants;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Debt implements DebtsListItem {
    private final int myId;
    private final Person myPerson;
    private final Subject mySubject;
    private final int myQuantity;
    private final int myRecordType;
    private final Date myDate;
    private final String myComment;
    private final List<PartialPayment> myPartialPayments;
    private int myRemainder;

    public Debt(final int id,
                final Person person,
                final Subject subject,
                final int quantity,
                final int recordType,
                final Date date,
                final String comment) {
        myId = id;
        myPerson = person;
        mySubject = subject;
        myQuantity = quantity;
        myRecordType = recordType;
        myDate = date;
        myComment = comment;
        myPartialPayments = new ArrayList<PartialPayment>();
        myRemainder = myQuantity;
    }

    public int getId() {
        return myId;
    }

    public Person getPerson() {
        return myPerson;
    }

    public Subject getSubject() {
        return mySubject;
    }

    public int getQuantity() {
        return myQuantity;
    }

    public int getRecordType() {
        return myRecordType;
    }

    public Date getDate() {
        return myDate;
    }

    public String getComment() {
        return myComment;
    }

    public List<PartialPayment> getPartialPayments() {
        return myPartialPayments;
    }

    public void addPartialPayment(final PartialPayment partialPayment) {
        myPartialPayments.add(partialPayment);
        myRemainder -= partialPayment.getQuantity();
    }

    public int getRemainder() {
        return myRemainder == 0 ? myQuantity : myRemainder;
    }

    public boolean isSummaryItem() {
        return false;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder(myRecordType == Constants.LEND ? "+" : "-");
        sb.append(getRemainder());
        sb.append(" ");
        sb.append(mySubject);
        return sb.toString();
    }

    @Override
    public int hashCode() {
        return myId;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (!(obj instanceof Debt)) return false;

        final Debt other = (Debt) obj;
        return myId == other.myId;
    }
}
